package com.zboot.zbootadmin.project.system.role.controller;


import com.zboot.zbootadmin.common.annotation.Log;
import com.zboot.zbootadmin.common.constant.HttpStatus;
import com.zboot.zbootadmin.common.domain.ResponseData;
import com.zboot.zbootadmin.common.security.service.TokenService;
import com.zboot.zbootadmin.common.utils.ExcelUtils;
import com.zboot.zbootadmin.project.system.menu.service.SysRoleMenuService;
import com.zboot.zbootadmin.project.system.role.entity.SysRole;
import com.zboot.zbootadmin.project.system.role.entity.dto.RoleDto;
import com.zboot.zbootadmin.project.system.role.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author zyt
 * @since 2020-07-18
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysRoleMenuService roleMenuService;
    /**
     *
     * @Description: 带条件查询，如果条件为空，则查全部
     *
     * @author: zyt
     * @date: 21:23 2020/9/11
     * @param: [roleDto]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("查询角色")
    @PreAuthorize("@zboot.hasPermi('system:role:list')")
    @PostMapping("/queryRoleList")
    public ResponseData queryRoleList(@RequestBody RoleDto roleDto) {

        return new ResponseData(HttpStatus.SUCCESS,roleService.queryRoleList(roleDto));
    }

    /**
     *
     * @Description: 添加角色
     *
     * @author: zyt
     * @date: 8:45 2020/9/14
     * @param: [roleDto]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("添加角色")
    @PreAuthorize("@zboot.hasPermi('sys:role:add')")
    @PostMapping("/addRole")
    public ResponseData addRole(@RequestBody SysRole sysRole) {
       roleService.insertRole(sysRole);
        return new ResponseData(HttpStatus.SUCCESS,"新增成功");
    }

    /**
     *
     * @Description: 删除角色
     *
     * @author: zyt
     * @date: 22:18 2020/9/19
     * @param: [sysRole]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("删除角色")
    @PreAuthorize("@zboot.hasPermi('sys:role:delete')")
    @DeleteMapping("/deleteRole")
    public ResponseData deleteRole(@RequestBody SysRole sysRole) {

        roleService.deleteRole(sysRole.getRoleIds().stream().toArray(String[]::new));
        return new ResponseData(HttpStatus.SUCCESS,"删除成功");
    }

    /**
     *
     * @Description: 导出角色
     *
     * @author: zyt
     * @date: 22:18 2020/9/19
     * @param: [response]
     * @return: void
     *
     */
    @Log("导出角色")
    @PreAuthorize("@zboot.hasPermi('sys:role:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response) {

        String [] titles = {"角色名称","角色权限字符串","角色状态（0正常 1停用）",
                "创建者"};
        List<SysRole> userList = roleService.list();
        ExcelUtils<SysRole> excelUtils = new ExcelUtils<>();
        String [] properties = {"roleName","roleKey","status","createBy"};
        excelUtils.downExcel(titles,userList,response,SysRole.class,properties);

    }

    /**
     *
     * @Description: 根据id获取角色信息
     *
     * @author: zyt
     * @date: 14:13 2020/9/20
     * @param: [roleId]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("获取角色信息")
    @PreAuthorize("@zboot.hasPermi('sys:role:edit')")
    @GetMapping("/queryRoleById/{roleId}")
    public ResponseData queryRoleById(@PathVariable("roleId") String roleId) {

        return new ResponseData(HttpStatus.SUCCESS,roleService.queryRoleById(roleId));
    }

    /**
     *
     * @Description: 编辑角色信息
     *
     * @author: zyt
     * @date: 14:14 2020/9/20
     * @param: [sysRole]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("编辑角色")
    @PutMapping("/editRole")
    public ResponseData editRole(@RequestBody SysRole sysRole) {
        roleService.editRole(sysRole);
        return new ResponseData(HttpStatus.SUCCESS,"修改成功");
    }



}

